package cn.keniykey.mc.landterritory.landview;

import java.util.ArrayList;
import java.util.LinkedList;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration.ViewConfig;
import cn.keniykey.mc.landterritory.land.LandManager;
import cn.keniykey.mc.landterritory.land.api.ELandType;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.viewlibrary.container.api.EContainerType;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewIntent;
import cn.keniykey.mc.viewlibrary.view.api.IViewClickHandler;
import cn.keniykey.mc.viewlibrary.view.api.ViewClickReport;

public final class LandQueryView extends AbLandFrameView{

	protected LandQueryView(Player player1) {
		super(player1);
	}
	
	private static final String VIEW_NAME = "landquery";
	
	private OfflinePlayer offplayer;
	
	@Override
	protected void onCreate() {
		super.onCreate();
		this.offplayer = this.getIntent().getExtras().getValue("player", this.getPlayer());
		
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		this.setViewType(EContainerType.matchContainer(config_view.getViewSize()));
		this.setTitle(Util.conver(config_view.getViewTitle()));
		
		final String name = this.offplayer.getName();
		//查询领地
		ItemStack item = coverItem(config_view.getViewItem("other_territory"), name);
		this.setIconClick(config_view.getViewItemSlot("other_territory"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandQueryView.this.isMatchClickSpeed()) return;
				final LinkedList<Land> linkes_lands = new LinkedList<>();
				for(final Land land : LandManager.getLandManager().getPlayerLands(LandQueryView.this.offplayer.getUniqueId())) {
					if(ELandType.TERRITORY.equals(land.getType())) linkes_lands.add(land);
				}
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", new ArrayList<>(linkes_lands));
				linkes_lands.clear();
				intent.putExtras(fvb);
				LandQueryView.this.showView(intent);
			}
		});
		//查询小区
		item = coverItem(config_view.getViewItem("other_community"), name);
		this.setIconClick(config_view.getViewItemSlot("other_community"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandQueryView.this.isMatchClickSpeed()) return;
				final LinkedList<Land> linkes_lands = new LinkedList<>();
				for(final Land land : LandManager.getLandManager().getPlayerLands(LandQueryView.this.offplayer.getUniqueId())) {
					if(ELandType.COMMUNITY.equals(land.getType())) linkes_lands.add(land);
				}
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", new ArrayList<>(linkes_lands));
				linkes_lands.clear();
				intent.putExtras(fvb);
				LandQueryView.this.showView(intent);
			}
		});
		//查询地皮
		item = coverItem(config_view.getViewItem("other_ground"), name);
		this.setIconClick(config_view.getViewItemSlot("other_ground"), item, new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!LandQueryView.this.isMatchClickSpeed()) return;
				final LinkedList<Land> linkes_lands = new LinkedList<>();
				for(final Land land : LandManager.getLandManager().getPlayerLands(LandQueryView.this.offplayer.getUniqueId())) {
					if(ELandType.GROUND.equals(land.getType())) linkes_lands.add(land);
				}
				final FrameViewIntent intent = new FrameViewIntent(LandListView.class);
				final FrameViewBundle fvb = new FrameViewBundle();
				fvb.putValue("lands", new ArrayList<>(linkes_lands));
				linkes_lands.clear();
				intent.putExtras(fvb);
				LandQueryView.this.showView(intent);
			}
		});
	}
	private ItemStack coverItem(ItemStack item, String name) {
		final ItemMeta meta = item.getItemMeta();
		meta.setDisplayName(Util.setStr(meta.getDisplayName(), name));
		item.setItemMeta(meta);
		return item;
	}
}